package com.wwj.my_ummary.util.ett;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;

    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String formatDate(String time) {
        Date date = null;
        try {
            date = format.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        long delta = new Date().getTime() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        if (delta < 30L * ONE_DAY) {
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }
        if (delta < 12L * 4L * ONE_WEEK) {
            long months = toMonths(delta);
            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        } else {
            long years = toYears(delta);
            return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
        }
    }

    //检查OTC交易时间
    public static boolean checkOtcTime(int start, int stop) throws ParseException {
        boolean isflag = false;
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        int hour = date.get(Calendar.HOUR_OF_DAY);
        if (hour >= start && hour < stop) {
            isflag = true;
        }
        return isflag;
    }

    //随意存是否到达释放时间点
    public static boolean isAllowRelease() {
        boolean isflag = false;
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        int hour = date.get(Calendar.HOUR_OF_DAY);
        if (14 <= hour && hour <= 16) {
            isflag = true;
        }
        return isflag;
    }

    /**
     * Description:格式化日期,String字符串转化为Date
     *
     * @param date
     * @param dtFormat 例如:yyyy-MM-dd HH:mm:ss yyyyMMdd
     * @return
     */
    public static String fmtDateToStr(Date date, String dtFormat) {
        if (date == null)
            return "";
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(dtFormat);
            return dateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    //获取结束倒计时/秒
    public static long getDWCountdownSecends(String time) {
        long countdownmain = 24 * 60;
        //获取随单开始的时间(双方匹配时间)
        try {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            calendar.setTime(simpleDateFormat.parse(time));
            calendar.add(Calendar.HOUR, 24);//订单倒计时
            countdownmain = DateUtil.dateDiff(DateUtil.format.format(calendar.getTime()), 2);
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return countdownmain;
    }

    public static Date getDate(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }


    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    //获取两个时间相差多少天
    public static double getBetweenDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {
            if (year2 < year1) {
                return 0;
            } else {
                //不同年
                int timeDistance = 0;
                for (int i = year1; i < year2; i++) {
                    if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                    {
                        timeDistance += 366;
                    } else    //不是闰年
                    {
                        timeDistance += 365;
                    }
                }
                return timeDistance + (day2 - day1);
            }
        } else {
            //同年
            return day2 - day1;
        }
    }

    //比较两个时间,如果比当前时间大为true反之
    public static boolean compareNowDate(String date) throws ParseException {
        return format.parse(date).compareTo(new Date()) > 0;
    }

    public static boolean investmentIsOpen(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return new Date().compareTo(sdf.parse(date)) > 0;
    }

    /**
     * 获取与系统时间的差值
     * eg. dateDiff(birth, today, Calendar.MONTH) 孩子的月龄
     *
     * @param sDate    开始时间
     * @param eDate    结束时间
     * @param diffType 日期类型
     * @return 根据 diffType计算的 eDate - sDate 时差
     * @throws ParseException
     */
    public static Long dateDiff(String sDate, int diffType) throws ParseException {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(new Date());
        caled.setTime(format.parse(sDate));
        long diffMill = caled.getTime().getTime() - calst.getTime().getTime();
        long rtn = 0;
        switch (diffType) {
            case 0:
                rtn = diffMill / 1000 / 3600;
                break;
            case 1:
                rtn = diffMill / 1000 / 60;
                break;
            case 2:
                rtn = diffMill / 1000;
                break;
        }
        return rtn;
    }


    public static Long dateDiff(String sDate, String eDate, int diffType) throws ParseException {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(format.parse(sDate));
        caled.setTime(format.parse(eDate));
        long diffMill = caled.getTime().getTime() - calst.getTime().getTime();
        long rtn = 0;
        switch (diffType) {
            case 0:
                rtn = diffMill / 1000 / 3600;
                break;
            case 1:
                rtn = diffMill / 1000 / 60;
                break;
            case 2:
                rtn = diffMill / 1000;
                break;
        }
        return rtn;
    }
    
  /*  public static Long releaseDiff(String lasttime) throws ParseException {
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	//获取释放时间
        java.util.Calendar calst = java.util.Calendar.getInstance();  
        calst.setTime(format.parse(lasttime));
        //释放时间后推24小时
        java.util.Calendar caled = java.util.Calendar.getInstance();  
        calst.setTime(format.parse(lasttime)); 
        caled.add(Calendar.MINUTE,60*24);
        //获取两个时间差
        long diffMill = caled.getTime().getTime() - calst.getTime().getTime();
        long rtn = diffMill / 1000 / 60;
        return rtn;
       
    }*/


    public static void main(String[] args) throws ParseException, FileNotFoundException, ScriptException, Exception {
		/*Calendar calendar = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取随单开始的时间(双方匹配时间)
		calendar.setTime(simpleDateFormat.parse("2019-04-07 11:49:17"));
		calendar.add(Calendar.HOUR, 2);
		long countdownmain=DateUtil.dateDiff(DateUtil.format.format(calendar.getTime()), 1);
		System.out.println(countdownmain);*/

        ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
        /*
         * ClassLoader classLoader = Global.class.getClassLoader(); URL resource =
         * classLoader.getResource("javascript"); String path = resource.getPath();
         */
        String script = "C:\\Users\\admin002\\Desktop\\JavaCode\\ZET\\WebContent\\js\\bip39\\transfer\\ethtransfer.js";
        engine.eval(new FileReader(script));
        Invocable invocable = (Invocable) engine;
        Object result = invocable.invokeFunction("sendEthTrasfer");
        System.out.println(result);

    }

    /**
     * 获取到下个10分钟的间隔
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static long getNextTimestamp(Date time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c1 = Calendar.getInstance();
        c1.setTime(time);
        String minute = String.valueOf(c1.get(Calendar.MINUTE));
        int min = minute.substring(minute.length() - 1).equals("0") ? Integer.parseInt(minute) + 10 : Integer.parseInt(minute) + (10 - Integer.parseInt(minute.substring(minute.length() - 1)));
        Calendar c2 = Calendar.getInstance();
        c2.setTime(time);
        c2.set(Calendar.MINUTE, min);
        c2.set(Calendar.SECOND, 0);
        return dateDiff(sdf.format(c1.getTime()), sdf.format(c2.getTime()), 2);
    }

    /**
     * 获取到当天结束还有多少秒
     *
     * @return
     */
    public static Integer getDifferSecend() {
        Calendar midnight = Calendar.getInstance();
        midnight.add(Calendar.DAY_OF_MONTH, 1);
        midnight.set(Calendar.HOUR_OF_DAY, 0);
        midnight.set(Calendar.MINUTE, 0);
        midnight.set(Calendar.SECOND, 0);
        midnight.set(Calendar.MILLISECOND, 0);
        Integer seconds = (int) ((midnight.getTime().getTime() - new Date().getTime()) / 1000);
        return seconds;
    }


    public static String subMonth(Date date, int count, String sdff) {
        SimpleDateFormat sdf = new SimpleDateFormat(sdff);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, count);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }


    public static String getSjCN(String time) {
        String sj = "";
        if (time != null) {
            String[] split = time.split("-");
            sj = split[0] + "年" + split[1] + "月" + split[2] + "日";
        }
        return sj;
    }

    //判定是否为工作日
    public static boolean isWorkDay(int shour, int ehour) {
        Calendar d = new GregorianCalendar();
        d.setTime(new Date());
        int w = d.get(Calendar.DAY_OF_WEEK) - 1;
        return w != 0 && w != 6 && shour <= d.get(Calendar.HOUR_OF_DAY) && d.get(Calendar.HOUR_OF_DAY) <= ehour;
    }

    public static String getCNSJ(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(date);
    }

    public static String formatDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat(format);
        try {
            return sdf2.format(sdf.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 秒转换为指定格式的日期
     *
     * @param minutes
     * @param patten
     * @return
     */
    public static String formateDateByPattern(long minutes, String patten) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(minutes * 60 * 1000);//转换为毫秒
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patten);
        String dateString = format.format(date);
        return dateString;
    }

    /**
     * 格式化当前时间  YYYY-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String currentDate() {
        return format.format(new Date());
    }
}
